<template>
  <!-- 筛选区 -->
  <div class="sub-filter">
    <!--品牌  -->
    <div class="item">
      <div class="head">品牌：</div>
      <div class="body">
        <a
          :class="{ active: i.selected }"
          @click="setBrand(i)"
          href="javascript:;"
          v-for="i in subCates.brands"
          :key="i.id"
          >{{ i.name }}</a
        >
      </div>
    </div>
    <!--规格(可能有多个)  -->
    <div class="item" v-for="item in subCates.saleProperties" :key="item.id">
      <div class="head">{{ item.name }}：</div>
      <div class="body">
        <!-- <a href="javascript:;">全部</a> -->
        <a
          :class="{ active: proper.selected }"
          href="javascript:;"
          v-for="proper in item.properties"
          @click="setproper(proper, item)"
          :key="proper.id"
          >{{ proper.name }}</a
        >
      </div>
    </div>
  </div>
</template>
<script>
import { inject } from 'vue'
export default {
  name: 'SubFilter',
  setup () {
    const subCates = inject('subCates')
    console.log(19191991, subCates)
    // 获取过滤条件数据
    const filterParams = inject('filterParams')
    // 点击品牌高亮
    const setBrand = (brand) => {
      // 选择过就不继续向下执行了
      if (brand.selected) return
      // console.log(brand)
      filterParams.value.brandId = brand.id
      // 清除之前选中元素的高亮
      subCates.value.brands.forEach(
        item => {
          item.selected = false
        }
      )
      brand.selected = true
    }
    // 点击规格属性高亮
    const setproper = (proper, propers) => {
      if (proper.selected) return
      console.log(111, proper, propers)
      // 需求存储多个规格下选中的属性数据
      // filterParams.value.attrs  =[{groupName: "规格", propertyName: "CPE加厚手套-100只/盒"}]
      // 1 一个规格下只能存储一个属性
      // 2 判断一下是不是全部
      // 根据规格名,来判断当前规格下是否有存储的选中的属性
      const index = filterParams.value.attrs.findIndex(per => per.groupName === propers.name)
      if (index > -1) {
        // 当前点击规格之前有存储,需要删除
        filterParams.value.attrs.splice(index, 1)
      }
      if (proper.id !== null) {
        // 不是全部
        filterParams.value.attrs.push({
          groupName: propers.name,
          propertyName: proper.name
        })
      }
      propers.properties.forEach(item => {
        item.selected = false
      })
      proper.selected = true
    }

    return { subCates, setBrand, setproper }
  }
}
</script>
<style scoped lang='less'>
// 筛选区
.sub-filter {
  background: #fff;
  padding: 25px;
  .item {
    display: flex;
    padding: 10px 0;
    .head {
      width: 80px;
      color: #999;
    }
    .body {
      flex: 1;
      a {
        display: inline-block;
        margin-right: 36px;
        margin-bottom: 20px;
        transition: all 0.3s;
        border: 1px solid #eee;
        padding: 6px;
        &.active,
        &:hover {
          color: @xtxColor;
          border-color: @xtxColor;
        }
      }
    }
  }
}
</style>
